/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_seek_config_manager.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {

SleSeekConfigManager::SleSeekConfigManager()
{
    SLE_ASSERT_AND_RETURN(fileName_.empty() == false);
    seekCfg_ = std::make_unique<SleSeekParam>();
    sleCfgMgr_ = std::make_unique<SleConfigManager>(fileName_);
    SLE_ASSERT_AND_RETURN_LOG(sleCfgMgr_ != NULL, "create sleCfgMgr_ fail");
    SLE_ASSERT_AND_RETURN(GetConfigFromValues() == SLE_NO_ERROR);
}

SleSeekConfigManager::SleSeekConfigManager(std::string fileName)
{
    SLE_ASSERT_AND_RETURN(fileName.empty() == false);
    fileName_ = fileName;
    seekCfg_ = std::make_unique<SleSeekParam>();
    sleCfgMgr_ = std::make_unique<SleConfigManager>(fileName_);
    SLE_ASSERT_AND_RETURN_LOG(sleCfgMgr_ != NULL, "create sleCfgMgr_ fail");
    SLE_ASSERT_AND_RETURN(GetConfigFromValues() == SLE_NO_ERROR);
}

SleSeekConfigManager::~SleSeekConfigManager()
{
//    SleErrorCode ret = SetConfigToValues();
//    SLE_ASSERT_AND_RETURN(ret == SLE_NO_ERROR);
//    SLE_ASSERT_AND_RETURN(SetConfigToValues() == SLE_NO_ERROR);
}

SleErrorCode SleSeekConfigManager::GetSeekConfig(SleSeekParam &seekConfig)
{
    SLE_ASSERT_AND_RETURN_RET(seekCfg_ != NULL, SLE_ERR_NOT_READY);
    seekConfig = *seekCfg_.get();
    return SLE_NO_ERROR;
}

SleErrorCode SleSeekConfigManager::GetConfigFromValues()
{
    SleConfigValueMap valueMap;
    SleErrorCode ret = sleCfgMgr_->GetValue(valueMap);
    std::string modeStr = "sle_seek_config";
    SleConfigValue values = valueMap[modeStr];
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    seekCfg_->addrType = sleCfgMgr_->Str2Uint8(values["addrType"]);
    seekCfg_->repateFilter = sleCfgMgr_->Str2Uint8(values["repateFilter"]);
    seekCfg_->filterPolicy = sleCfgMgr_->Str2Uint8(values["filterPolicy"]);
    seekCfg_->phys = sleCfgMgr_->Str2Uint8(values["phys"]);
    seekCfg_->type[0] = sleCfgMgr_->Str2Uint8(values["type0"]);
    seekCfg_->interval[0] = sleCfgMgr_->Str2Uint16(values["interval0"]);
    seekCfg_->window[0] = sleCfgMgr_->Str2Uint16(values["window0"]);
    seekCfg_->type[1] = sleCfgMgr_->Str2Uint8(values["type1"]);
    seekCfg_->interval[1] = sleCfgMgr_->Str2Uint16(values["interval1"]);
    seekCfg_->window[1] = sleCfgMgr_->Str2Uint16(values["window1"]);
    seekCfg_->type[2] = sleCfgMgr_->Str2Uint8(values["type2"]);
    seekCfg_->interval[2] = sleCfgMgr_->Str2Uint16(values["interval2"]);
    seekCfg_->window[2] = sleCfgMgr_->Str2Uint16(values["window2"]);
    return SLE_NO_ERROR;
}

SleErrorCode SleSeekConfigManager::SetConfigToValues()
{
    SleConfigValue values;
    values["addrType"] = std::to_string(seekCfg_->addrType);
    values["repateFilter"] = std::to_string(seekCfg_->repateFilter);
    values["filterPolicy"] = std::to_string(seekCfg_->filterPolicy);
    values["phys"] = std::to_string(seekCfg_->phys);
    values["type"] = std::to_string(seekCfg_->type[0]);
    values["interval"] = std::to_string(seekCfg_->interval[0]);
    values["window"] = std::to_string(seekCfg_->window[0]);

    std::string modeStr = "sle_seek_config";
    SleConfigValueMap valueMap;
    valueMap[modeStr] = values;
    SleErrorCode ret = sleCfgMgr_->SetValue(valueMap);
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

} // SleStandard
} // NearLink
} // OHOS